// Google Buzz for GSA Core JS will load the object GBuzzGSA into your window
// that can be used to automatically fetch buzz results based on your query

// Written by Steve King <steve@sming.com.au>
// Last Modified Monday 7th June, 2010

var GBuzzGSA;
(function(){
	this.core = function() {
		// What do you want to return search on
		// This is automatically updated based on the query string in the URL;
		this.q = false;
		
		// Dimension information
		this.height = false;
		this.width = false;
		
		// Defaults
		// Where do we go to get the search data?
		this.buzzURL = 'https://www.googleapis.com/buzz/v1/activities/search?alt=json&callback=GBuzzGSA.update';
		// How many, by default, do we limit the search to (can be modified)
		this.limit = 10;
		
		// This is the location of the Prototype lib - we need this for some array and object functions
		this.c = 'http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js';
		// Location of the CSS File;
		// Alternatively, you can cut/paste the CSS into your doc.
		this.e = 'buzzcoreGSA.css';
		
		// Initialise the search and load the results
		// Run this function as soon as you are ready (can be at the bottom of the page, for example
		// To reduce load times of main results
		this.load = function() {
			{try{
				if(this.Zc){
					if(this.q != false) {
						this.fetch(this.buzzURL + '&q=' + this.q + '&max-results=' + this.limit);
					}
					if(this.width!=false){this.Zc.style.width=this.width;this.Zc.style.display='block'}
				}
			}
			// If there is a failure in doing this, we do nothing!
			catch(e){}};
		};
		
		// Fetch the Buzz Feeds
		this.fetch = function(a) {
			{try{if(this.a){
				if (this.b){
					this.a.removeChild(this.b);
				}
			}else{
				this.a = document.getElementsByTagName('head')[0];
			}
			}catch(e){
				this.a = document.getElementsByTagName('head')[0];
			}};
			this.b = document.createElement('script');
			this.b.type = 'text/javascript';
			this.b.src = a;
			this.a.appendChild(this.b);
		};
		
		// Dynamically load Prototype and CSS into the browser header;
		this.inc = function(a) {
			{try{if(this.a){
			}else{
				this.a = document.getElementsByTagName('head')[0];
			}
			}catch(e){
				this.a = document.getElementsByTagName('head')[0];
			}};
			var b = document.createElement('script');
			b.type = 'text/javascript';
			b.src = a;
			
			var c = document.createElement('link');
			c.type = 'text/css';
			c.href = this.e;
			c.rel = 'stylesheet';
			this.a.appendChild(c);
			this.a.appendChild(b);
		};
		
		
		// The buzzObject Class for a buzz entry
		this.buzzObject = function(a) {
			this.a = a;
			
			// What kind of object is this, ie buzz#activityfeed
			this.kind = function() {
				a = false;
				if(this.a ){if(this.a.kind){ 
					a = this.a.kind;
				}};
				return a;
			};
			
			// Return the title of the function
			this.title = function() {
				a = false;
				if(this.a ){if(this.a.title){ 
					a = this.a.title;
				}};
				return a;
			};

			// Return the formatted, published timestamp;
			this.published = function() {
				a = false;
				var b = "{'01':'Jan','02':'Feb','03':'Mar','04':'Apr','05':'May','06':'June','07':'Jul','08':'Aug','09':'Sep','10':'Oct','11':'Nov','12':'Dec'}";
				b = b.evalJSON();
				if(this.a ){if(this.a.published){ 
					var c = this.a.published.split('T');
					var g = c[1];
					c = c[0].split('-');
					g = g.split('.');
					g = g[0];
					var d = c[0];
					var e = c[1];
					var f = c[2];
					a = f + '-' + b[e] + ' ' + d + ' at ' + g;
				}};
				return a;
			};
			
			// Return the formatted, updated timestamp;
			this.updated = function() {
				a = false;
				var b = "{'01':'Jan','02':'Feb','03':'Mar','04':'Apr','05':'May','06':'June','07':'Jul','08':'Aug','09':'Sep','10':'Oct','11':'Nov','12':'Dec'}";
				b = b.evalJSON();
				if(this.a ){if(this.a.updated){ 
					var c = this.a.updated.split('T');
					var g = c[1];
					c = c[0].split('-');
					g = g.split('.');
					g = g[0];
					var d = c[0];
					var e = c[1];
					var f = c[2];
					a = f + '-' + b[e] + ' ' + d + ' at ' + g;
				}};
				return a;
			};
			
			// Return the ID of the buzz (google string)
			this.id = function() {
				a = false;
				if(this.a ){if(this.a.id){ 
					a = this.a.id;
				}};
				return a;
			};
			
			// Link to the URL of the buzz itself;
			this.link = function() {
				a = false;
				if(this.a ){if(this.a.links){if(this.a.links.alternate){if(this.a.links.alternate[0].href){ 
					a = this.a.links.alternate[0].href;
				}}}};
				return a;
			};
			
			// Replies count
			this.replies = function() {
				var a = 0;
				if (this.a.links){if(this.a.links.replies){if(this.a.links.replies[0].count>0){
					a = this.a.links.replies.count;
					//var b = this.a.links.replies[0].href;
					//var c = this.a.links.replies[0].updated;
				}}};
				return a;
			};
			
			// Return a HTML Formatted actor block for this item;
			this.actor = function() {
				var a = false;
				// Return the id, name, profile, thumb
				if (this.actorImage()!=false){
					a = '<a href="' + this.actorprofile() + '" target="_blank">';
					a = a + this.actorImage();
					a = a + '</a>';
				};
				if(this.actorname()!=false){
					if(a==false) {
						a = '';
					};
					a = a + '<p class="gsaBuzzActor"><a href="' + this.actorprofile() + '" target="_blank">';
					a = a + this.actorname();
					a = a + '</a></p>';
				};
				return a;
			};
			
			// Return the actors image as HTML, or default if none
			this.actorImage = function() {
				var a = false;				
				if (this.a.actor){
					if(this.a.actor.thumbnailUrl){
						a = '<img src="' + this.a.actor.thumbnailUrl + '" />';
					}
					else {
						a = '<img src="http://www.gstatic.com/s2/tt/landing/landing_taco.png" />';
					}
				};
				return a;
			};
			
			// Return the actor ID
			this.actorid = function() {
				var a = false;
				if(this.a.actor){if(this.a.actor.id){
					a = this.a.actor.id;
				}};
				return a;
			};
			
			// Return the actors name (public name)
			this.actorname = function() {
				var a = false;
				if(this.a.actor){if(this.a.actor.name){
					a = this.a.actor.name;
				}};
				return a;
			};
			
			// Return the actors profile URL
			this.actorprofile = function() {
				var a = false;
				if(this.a.actor){if(this.a.actor.profileUrl){
					a = this.a.actor.profileUrl;
				}};
				return a;
			};
			
			// Return the thumbnail location of the buzz
			// if there is one.
			this.actorthumb = function() {
				var a = false;
				if(this.a.actor){if(this.a.actor.thumbnailUrl){
					a = this.a.actor.thumbnailUrl;
				}};
				return a;
			};
			
			// Return the type of the buzz, ie Note
			this.objectType = function() {
				var a = false;
				if(this.a.object){if(this.a.object.type){
					a = this.a.object.type;
				}};
				return a;
			};
			
			// Return the full content of the buzz;
			this.objectContent = function() {
				var a = false;
				if(this.a.object){if(this.a.object.content){
					a = this.a.object.content;
				}};
				return a;
			};
			
			// Return false, or the source product of the post;
			this.source = function() {
				var a = false;
				if(this.a.source){if(this.a.source.title){
					a = this.a.source.title;
				}};
				return a;
			};
			
			// Return false, or the lat/long of the post;
			this.geo = function() {
				var a = false;
				if(this.a.geocode){
					a = this.a.geocode;
				};
				return a;
			};
			
			// Return false, or the address of the post;
			this.address = function() {
				var a = false;
				if (this.a.address) {
					a = this.a.address;
				};
				return a;
			};
			
			// Return false, or the place name of the post;
			this.place = function() {
				var a = false;
				alert(this.a);
				if (this.a.placeName){
					a = this.a.placeName;
				};
				return a;
			};
			
			// This function is automatically run when the object is created
			// It loads the result into the Buzz main Div
			this.invokeOn = function() {
				if(this.Zc){
					this.Zd = '<div class="gsaBuzzItem">';
					if (this.actor()!=false) {
						this.Zd = this.Zd + this.actor();
					}
					if ( this.title()!=false) {
						this.Zd = this.Zd + '<p class="gsaBuzzTitle">' + this.title() + '</p>';
					}
					this.Zd = this.Zd + '<p class="gsaBuzzActor">';
					if(this.source()!=false){
						this.Zd = this.Zd + ' via ' + this.source();
					}
					if(this.published()!=false) {
						this.Zd = this.Zd + ' on ' + this.published();
					}
					this.Zd = this.Zd + '</p>';
					this.Zd = this.Zd + '</div>';
					this.Zc.innerHTML = this.Zc.innerHTML + this.Zd;
				};
			};
			
			// Check to see if there is a gsaBuzzBox div, if so, set it as a pointer;
			{try{this.Zc = document.getElementById('gsaBuzzBox');}catch(e){}};
			// Once this object is fully loaded, automatically execute the updater;
			{try{this.invokeOn();}catch(e){}};
		};
		
		
		// This function checks to see if there is a query term in the URL
		this.Za = function(a) {
			a = a.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			var b = "[\\?&]"+a+"=([^&#]*)";
			var c = new RegExp(b);
			var d = c.exec(window.location.href);
			if(d == null){
				return "";
			}else{
				return d[1];
			}
		};
		
		// Try and load the Prototype Script into the browser;
		{try{this.inc(this.c);}catch(e){}};
		
		// This is the callback function from Buzz, which is executed when
		// the results hit the browser.
		this.update = function(a) {
			if(a){if(a.data){if(a.data.items){
				if(a.data['items'].length > 0) {
					if(this.Zc){
						this.Zc.innerHTML = '';
					};
					{try{a.data['items'].each(function(a,b) {
						if (!this.objects){
							this.objects = [];
						};
						this.objects.push(new this.buzzObject(a));
					}.bind(this));}catch(e){}};
				}}}}
		};
		
		// Try and set the document DIV and set the load message;
		{try{this.Zc = document.getElementById('gsaBuzzBox');if(this.Zc){this.Zc.innerHTML='Loading results...';}}catch(e){}};
		// If there is a ?q= param in the url? Set this to this.Zb
		{try{this.Zb = this.Za('q');}catch(e){this.Zb = false}};
		// If there is no error here, set the query term.
		if (this.Zb != false){this.q = this.Zb;};
	};
	
	GBuzzGSA = new this.core();
})();